// 检查是否登录，未登录跳转登录页，抛出异常中断后续操作
export function isLogin() {
  return new Promise((resolve, reject) => {
    let token = uni.getStorageSync('token');
    if (!token) {
      uni.reLaunch({
        url: '/pages/login/login'
      })
      return reject('未登录');
    }
    return resolve('已登录');
  });
}

export const TOKEN_NAME = 'cs-token';// 用户对象
export const USER_KEY = 'cs-user-key';// 用来无感登录
export const USER_DATA = 'cs-user-data';// 当前登录的对象
export const USER_LOGIN_TIME = 'cs-login-time';// 当前登录的时间
export const SYS_LANGUAGE_TIME = 'cs-sys-language';// 当前系统语言

/**
 * 设置token
 * @param {Object} token
 */
export function setStorageFun(key, value, sync = true){
	try {
		if (sync) {
			return uni.setStorageSync(key, value);
		} else {
			uni.setStorage({
				key: key,
				data: value
			});
		}
	} catch (e) {

	}
};

/**
 * 获取token
 */
export function getStorageFun(key,sync = true){
	try {
		if(sync){
			return uni.getStorageSync(key);
		}else{
			let data = '';
			uni.getStorage({
				key:key,
				success: function (res) {
					data = res.data;
				}
			});
			return data;
		}
	} catch (e) {
		return false;
	}
}

//移除
export function del(key, sync = true){
    try {
        if (sync) {
            return uni.removeStorageSync(key);
        } else {
            uni.removeStorage({
                key: key
            });
        }
    } catch (e) {
        return false;
    }
}

//清空
export function clear(sync = true){
    try {
        if (sync) {
            return uni.clearStorageSync();
        } else {
            uni.clearStorage();
        }
		uni.removeStorage({
			key: 'isForcedLogin',
		});
    } catch (e) {
        return false;
    }
}